IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[cms_sp_PageControlMove]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [dbo].[cms_sp_PageControlMove]
GO
create procedure [dbo].[cms_sp_PageControlMove]
@PageControlId int,
@IsMoveUp bit
as
declare @NeighbourPageControlId int
declare @TemplateRegionId int
declare @PageControlOrder int
select 
	 @TemplateRegionId = TemplateRegionId
from dbo.PageControls
where PageControlId = @PageControlId
select 
	@NeighbourPageControlId = PageControlId,
	@PageControlOrder = PageControlOrder
from dbo.PageControls
where (@IsMoveUp = 1 and PageControlOrder = 
	(select sub.PageControlOrder - 1 
	from dbo.PageControls sub
	where sub.PageControlId = @PageControlId) and TemplateRegionId = @TemplateRegionId) or 
	(@IsMoveUp = 0 and PageControlOrder = 
	(select sub.PageControlOrder + 1 
	from dbo.PageControls sub
	where sub.PageControlId = @PageControlId) and TemplateRegionId = @TemplateRegionId)
if @NeighbourPageControlId is not null
begin
	update dbo.PageControls
	set PageControlOrder = 
	(select sub.PageControlOrder
	from dbo.PageControls sub
	where sub.PageControlId = @PageControlId)
where PageControlId = @NeighbourPageControlId
update dbo.PageControls
	set PageControlOrder = @PageControlOrder
where PageControlId = @PageControlId
end


GO

